
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class K extends JFrame {
    private Mypenal mypenal=null;
    public static void main(String[] args) {
        new K();
    }
    public K(){
        mypenal=new Mypenal();
        this.add(mypenal);
        this.setSize(400,400);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addKeyListener(mypenal);
        this.setVisible(true);
    }
}
class Mypenal extends Panel implements KeyListener {
    int x=10;
    int y=10;
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillOval(x,y,20,20);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //System.out.println((char) e.getKeyCode()+"被按");
       switch (e.getKeyCode()){
           case KeyEvent.VK_S:y++;
           this.repaint();
           break;
           case KeyEvent.VK_W:y--;
           this.repaint();
           break;
           case KeyEvent.VK_D:x++;
           this.repaint();
           break;
           case KeyEvent.VK_A:x--;
           this.repaint();
               break;
       }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}